package com.example.verification.web;

import com.alibaba.fastjson.JSONObject;
import com.example.verification.model.User;
import com.example.verification.service.UserService;
import com.example.verification.utils.RetBack;
import com.example.verification.utils.SysTools;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rcl
 * @date 2019/7/29 10:12
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 个人中心，需认证可访问
     */
    @RequestMapping("/user/index")
    public String add(HttpServletRequest request) {
        User bean = (User) SecurityUtils.getSubject().getPrincipal();
        request.setAttribute("userName", bean.getName());
        return "/user/index";
    }

    @RequestMapping("/user/info")
    public String getInfo(HttpServletRequest request) {
        User bean = (User) SecurityUtils.getSubject().getPrincipal();
        request.setAttribute("userName", bean.getName());
        return "/user/info";
    }

    /**
     * 会员中心，需认证且角色为vip可访问
     */
//    @RequiresPermissions("user:test")
    @RequestMapping("/vip/index")
    public String update() {
        return "/vip/index";
    }

    @RequestMapping("/api/getInfo")
    @ResponseBody
    public RetBack getInfo(String ID) {
        List<User> users = userService.getList();
        return RetBack.success(users);
    }

    @RequestMapping("/api/getTableInfo")
    public String backTable() {
        return "/user/user_list";
    }

    /**
     * 新增用户
     *
     * @param user
     * @return com.example.verification.utils.RetBack
     * @author rcl
     * @date 2019/8/15 14:58
     */
    @PostMapping("/api/user")
    @ResponseBody
    public RetBack saveUser(User user) {
        String userId = userService.saveUser(user);
        return RetBack.success(userId);
    }

    /**
     * 根据id查找用户
     *
     * @param userId
     * @return com.example.verification.utils.RetBack
     * @author rcl
     * @date 2019/8/15 14:56
     */
    @GetMapping("/api/user/{userId}")
    @ResponseBody
    public RetBack getUserById(@PathVariable("userId") String userId) {
        User bean = (User) SecurityUtils.getSubject().getPrincipal();//获取当前登录用户
        System.out.println("userId = [" + userId + "]");
        return RetBack.success(bean);
    }

    /**
     * 根据条件查询用户列表（分页）
     *
     * @param pageNum, pageSize, userName, region
     * @return com.example.verification.utils.RetBack<com.alibaba.fastjson.JSONObject>
     * @author rcl
     * @date 2019/8/15 14:57
     */
    @GetMapping("/api/user")
    @ResponseBody
    public RetBack<JSONObject> getUserList(Integer pageNum, Integer pageSize, String userName, String region) {
        PageHelper.startPage(pageNum, pageSize);
        Map map = new HashMap<String, Object>();
        map.put("userName", userName);
        map.put("region", region);
        Page<User> userList = userService.getUserList(map);
        userList.getTotal();
        return ctrlBack(userList);
    }

    /**
     * 分页查询统一封装返回
     *
     * @param page
     * @return com.example.verification.utils.RetBack<com.alibaba.fastjson.JSONObject>
     * @author rcl
     * @date 2019/8/15 14:57
     */
    private RetBack<JSONObject> ctrlBack(Page page) {
        long pageNum = page.getPageNum();
        long pageSize = page.getPageSize();
        long total = page.getTotal();

        JSONObject json = new JSONObject();
        json.put("pageNum", pageNum);
        json.put("pageSize", pageSize);
        json.put("total", total);
        json.put("data", page);

        return RetBack.success(json);
    }
}
